package com.mjfeed.controller.shop;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.mjfeed.controller.BaseController;
import com.mjfeed.entity.BrowseLog;
import com.mjfeed.entity.Member;
import com.mjfeed.entity.Product;
import com.mjfeed.entity.ProductCategory;
import com.mjfeed.service.BrowseLogService;
import com.mjfeed.service.MemberService;
import com.mjfeed.service.ProductCategoryService;
import com.mjfeed.service.ProductService;
import com.mjfeed.util.Format;
import com.mjfeed.util.Pagination;
import com.mjfeed.util.ParamBean;

/**
 * @description 商品
 * @author zhaominglei
 * @date 2015年1月2日
 */
@Controller("shopProductController")
@RequestMapping("/shop/product")
public class ProductController extends BaseController {
	@Resource(name="productService")
	private ProductService productService;
	@Resource(name="productCategoryService")
	private ProductCategoryService productCategoryService;
	@Resource(name="memberService")
	private MemberService memberService;
	@Resource(name="browseLogService")
	private BrowseLogService browseLogService;
	
	@RequestMapping(value = "/list" ,method=RequestMethod.GET)
	public String list(ParamBean paramBean, HttpServletRequest request, HttpServletResponse response, Model model){
		paramBean.setUrl(getBasePath(request) + "shop/product/list.shtml");
		int p = (Format.null2Zero(request.getParameter("p")) == 0) ? 1 : Format.null2Zero(request.getParameter("p"));
		paramBean.setPage(p);
		Pagination<Product> pagination = productService.getPagination(paramBean);
		request.setAttribute("pagination", pagination);
		request.setAttribute("paramBean", paramBean);
		ProductCategory productCategory = null;
		if (paramBean.getProductCategoryId() != null && paramBean.getProductCategoryId().intValue() > 0) {
			productCategory = productCategoryService.getById(ProductCategory.class, paramBean.getProductCategoryId());
		}
		request.setAttribute("productCategory", productCategory);
		request.setAttribute("productCategoryTree", productCategoryService.findTree());
		return "shop/product/list";
	}
	
	@RequestMapping(value = "/detail" ,method=RequestMethod.GET)
	public String list(Long productId, HttpServletRequest request, HttpServletResponse response, Model model){
		if (productId == null || productId.intValue() < 1) {
			return ERROR_VIEW;
		}
		Product product = productService.getById(Product.class, productId);
		if (product == null) {
			return ERROR_VIEW;
		}
		//浏览日志
		Member member = memberService.getCurrent();
		if (member != null) {
			BrowseLog browseLog = new BrowseLog();
			browseLog.setMember(member);
			browseLog.setProduct(product);
			browseLog.setIp(getRemoteIpAddr(request));
			browseLogService.save(browseLog);
		}
		//所有重量商品
		String allWeightSn = product.getAllWeightSn();
		List<Product> wpList = new ArrayList<Product>();
		if (allWeightSn != null && !allWeightSn.equals("")) {
			String[] sns = allWeightSn.split(Product.PRODUCT_SN_SEPARATOR);
			for (String sn : sns) {
				Product p = productService.getProductBySn(sn);
				if (p == null) continue;
				wpList.add(p);
			}
			Collections.sort(wpList, new Comparator<Product>() {
				@Override
				public int compare(Product p1, Product p2) {
					return p1.getWeight() - p2.getWeight();
				}
			});
		}
		//推荐配件商品
		String recommendPartsSn = product.getRecommendPartsSn();
		List<Product> ppList = new ArrayList<Product>();
		if (recommendPartsSn != null && !recommendPartsSn.equals("")) {
			String[] sns = recommendPartsSn.split(Product.PRODUCT_SN_SEPARATOR);
			for (String sn : sns) {
				if (sn.equals(product.getSn())) continue;
				Product p = productService.getProductBySn(sn);
				if (p == null) continue;
				ppList.add(p);
			}
		}
		//推荐组合商品
		String recommendGroupSn = product.getRecommendGroupSn();
		List<Product> gpList = new ArrayList<Product>();
		if (recommendGroupSn != null && !recommendGroupSn.equals("")) {
			String[] sns = recommendGroupSn.split(Product.PRODUCT_SN_SEPARATOR);
			for (String sn : sns) {
				if (sn.equals(product.getSn())) continue;
				Product p = productService.getProductBySn(sn);
				if (p == null) continue;
				gpList.add(p);
			}
		}
		request.setAttribute("wpList", wpList);
		request.setAttribute("ppList", ppList);
		request.setAttribute("gpList", gpList);
		request.setAttribute("product", product);
		request.setAttribute("productCategoryTree", productCategoryService.findTree());
		return "shop/product/detail";
	}
}
